package com.chiang.viewpagerfragment2;

import java.util.ArrayList;

import com.fmstech.viewpagerfragment.R;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.PagerTitleStrip;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import android.os.Build;

public class MainActivity extends FragmentActivity implements OnClickListener{

	private ViewPager mViewPager;
	private ImageView mCursor;
	private Fragment mVideoFragment;
	private Fragment mMusicFragment;
	private Fragment mLivetvFragment;
	private ArrayList<Fragment> mFragments;
	private TextView mVideoText,mLivetvText,mMusicText;
	private int offset = 0;// ����ͼƬƫ����   
    private int currIndex = 0;// ��ǰҳ�����   
    private int bmpWidth;// ����ͼƬ���  
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		InitImageView();  
        InitTextView();   
        InitViewPager();   
		
	}

	private void InitViewPager() {
		
		mViewPager=(ViewPager)findViewById(R.id.viewpager);
		mVideoFragment=new VideoFragment();
		mLivetvFragment=new LivetvFragment();
		mMusicFragment=new MusicFragment();
		mFragments=new ArrayList<Fragment>();
		mFragments.add(mVideoFragment);
		mFragments.add(mLivetvFragment);
		mFragments.add(mMusicFragment);
		mViewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
		mViewPager.setCurrentItem(0);
//		mViewPager.setOffscreenPageLimit(1);
		mViewPager.setPageMargin(10);
		mViewPager.setOnPageChangeListener(new MyOnPageChangeListener());
	}

	private void InitImageView() {
		mCursor=(ImageView)findViewById(R.id.cursor);
		//�������Ŀ��
        bmpWidth = BitmapFactory.decodeResource(getResources(), R.drawable.strip).getWidth();// ��ȡͼƬ���   
        DisplayMetrics dm = new DisplayMetrics();  
        getWindowManager().getDefaultDisplay().getMetrics(dm);  
        int screenW = dm.widthPixels;// ��ȡ�ֱ��ʿ��   
        offset = (screenW / 3 - bmpWidth) / 2;// ����ƫ����   
        Matrix matrix = new Matrix();  
        matrix.postTranslate(offset, 0);  
        mCursor.setImageMatrix(matrix);// ���ö�����ʼλ��
	}

	private void InitTextView() {
		mVideoText=(TextView)findViewById(R.id.txt_video);
		mLivetvText=(TextView)findViewById(R.id.txt_livetv);
		mMusicText=(TextView)findViewById(R.id.txt_music);
		mVideoText.setOnClickListener(this);
		mLivetvText.setOnClickListener(this);
		mMusicText.setOnClickListener(this);
	}
	
	class MyPagerAdapter extends FragmentPagerAdapter {

		public MyPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public int getCount() {
			return mFragments.size();
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return super.isViewFromObject(view, object);
		}

		@Override
		public Fragment getItem(int arg0) {
			return mFragments.get(arg0);
		}


		class MyAdapter extends PagerAdapter{

			@Override
			public int getCount() {
				return 0;
			}

			@Override
			public boolean isViewFromObject(View arg0, Object arg1) {

				return false;
			}
			
		}
//		@Override
//		public CharSequence getPageTitle(int position) {
//			return mTitles.get(position);
//		}
	}
	
	/**
	 * ʵ��һ�������Ч��
	 * @author Jiangchun
	 *
	 */
	class MyOnPageChangeListener implements OnPageChangeListener{
		private int tapWidth=offset*2+bmpWidth;
		
		public MyOnPageChangeListener(){

		}
		
		@Override
		public void onPageScrollStateChanged(int arg0) {
		
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		
		}

		@Override
		public void onPageSelected(int arg0) {
			Animation animation = new TranslateAnimation(tapWidth*currIndex, tapWidth*arg0, 0, 0);//��Ȼ����Ƚϼ�ֻ࣬��һ�д��롣   
            currIndex = arg0;  
            animation.setFillAfter(true);// True:ͼƬͣ�ڶ�������λ��   
            animation.setDuration(250);  
            mCursor.startAnimation(animation);  
        }
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.txt_video:
			mViewPager.setCurrentItem(0);
			break;
		case R.id.txt_livetv:
			mViewPager.setCurrentItem(1);
			break;
		case R.id.txt_music:
			mViewPager.setCurrentItem(2);
			break;
		}
		
	}
}
